package unipd.esp1112.pro;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


/**
 * Activity where the user can view the detailed data of each single training session recorded with the current profile
 * 
 * @author andrea basso, federico lancerin, alex tomasello
 * 
 */
public class StoricoDettagli extends Activity {
	/**
	 * string containing the type of activity and the date of when it was recorded
	 */
	private String tipodata;
	/**
	 * an array of strings used to split "tipodata" in more useful strings
	 */
	private String[] tipodatasplit;
	/**
	 * string used to store the date of the running session
	 */
	private String datatext;
	/**
	 * textview used to display the date of the running session
	 */
	private TextView data;
	/**
	 * textview used to display the training type
	 */
	private TextView tipo;
	/**
	 * textview used to display the total calories burnt
	 */
	private TextView calorie;
	/**
	 * textview used to display the average speed
	 */
	private TextView velocita;
	/**
	 * textview used to display the total running time
	 */
	private TextView durata;
	/**
	 * textview used to display the total distance covered
	 */
	private TextView distanza;
	/**
	 * button used to delete the current training record from the database
	 */
	private Button elimina;
	/**
	 * variable used to retrieve an instance of the application private database
	 */
	private MyOpenHelper op;
	/**
	 * variable used to reference the database
	 */
	private SQLiteDatabase db;
	/**
	 * alertdialog that pops up when the user press "elimina" button to ask for a confirm before proceeding
	 */
	private AlertDialog.Builder builder;
	/**
	 * cursor used to retrieve the results of a query
	 */
	private Cursor query;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.historydetails);

		Intent intent = getIntent();
		data = (TextView) findViewById(R.id.dataStorico);
		tipo = (TextView) findViewById(R.id.tipoStorico);
		calorie = (TextView) findViewById(R.id.calorieStorico);
		velocita = (TextView) findViewById(R.id.velocitaStorico);
		durata = (TextView) findViewById(R.id.durataStorico);
		distanza = (TextView) findViewById(R.id.distanzaStorico);
		builder = new AlertDialog.Builder(this);

		// Mi faccio passare id e data scritti dentro il pulsante dello storico
		// che mi ha invocato. Poi ricavo solo id, che è univoco per ciascuna
		// riga della tabella, facendo uno split. Infine estraggo i dati che mi
		// servono dalla query e li metto nei textview.

		tipodata = intent.getStringExtra("tipodata");
		tipodatasplit = tipodata.split(" - ");
		datatext = tipodatasplit[1];
		System.out.println("'" + datatext + "'");
		op = new MyOpenHelper(this);
		db = op.getReadableDatabase();
		query = db.rawQuery("SELECT * FROM " + op.TABLE2 + " where " + op.DATAEORA + "='" + datatext + "'", null);
		query.moveToNext();
		data.setText(query.getString(1));
		tipo.setText(query.getString(2));
		distanza.setText(query.getString(3));
		durata.setText(query.getString(4));
		calorie.setText(query.getString(5));
		velocita.setText(query.getString(6));
		db.close();

		elimina = (Button) findViewById(R.id.eliminaAllenamento);
		elimina.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				builder.setMessage("Sicuro di voler eliminare questo allenamento e tutti i suoi dati?").setCancelable(false)
						.setPositiveButton("Si", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								db = op.getWritableDatabase();

								db.delete(op.TABLE2, op.DATAEORA + "='" + datatext + "'", null);
								db.close();
								Intent intent = new Intent();
								setResult(RESULT_OK, intent);
								finish();
								Toast toast = Toast.makeText(getApplicationContext(), "Allenamento del " + datatext + " cancellato.", Toast.LENGTH_SHORT);
								toast.setGravity(Gravity.CENTER, 0, 0);
								toast.show();
							}
						}).setNegativeButton("No", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});
				builder.create();
				builder.show();
			}
		});

	}

}
